//
//  HMNewFriend.swift
//  HealthMonitoring
//
//  Created by YTYanK on 2018/10/16.
//  Copyright © 2018年 YTYanK. All rights reserved.
//

import UIKit
import SwiftyJSON

class HMNewFriend: HMTableViewController {
     var nfJsonData = JSON()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        super.hideFooter()
    }
    
    func agianRequest() {
        NetRequestTool.shareInstance.met = .get
        NetRequestTool.request(url: "friend/requests", parameters: [:], success: { (json) in
            self.nfJsonData = json["data"]
           self.tableView.reloadData()
        }, fail: { (code, msg) in
            RKDropdownAlert.title("温馨提醒!", message: "获取数据失败（\(code)）！")
        }) {}
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        agianRequest()
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        
    }
    
    override func load() {
      self.title = "申请列表"
        self.tableView.register(FriendLogCell.classForCoder(), forCellReuseIdentifier: "FriendLogCell")
        
    }
    
    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let view = UIView()
        let label = UILabel()
        view.backgroundColor = configure.shareConfigure.HMColor2
        view.addSubview(label)
        label.leadingAlign(view: view, predicate: "10")
        label.centerYAlign(view: view)
        label.text = "申请列表"
        return view
    }
    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 30
    }
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.nfJsonData.count
    }
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 88
    }
    
    override func cellForRow(atIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = self.tableView.dequeueReusableCell(withIdentifier: "FriendLogCell") as! FriendLogCell
        cell.delegate = self
        cell.cellIndexPath = indexPath as IndexPath
        cell.isAcceptBtn = true
        cell.setJson(json: self.nfJsonData[indexPath.row])
        return cell
        
    }
    
    
}


extension HMNewFriend: FriendLogCellDelegate {
    func clickBtn(isAccept: Bool, selectRowAt indexPath: IndexPath, userId: String?) {
//        print("\(isAccept)------->>>\(String(describing: userId))",indexPath)
   
        if userId != nil {
            NetRequestTool.shareInstance.met = .post
            NetRequestTool.request(url: "friend/accept", parameters: ["friendId": userId!], success: { (json) in
                self.showToast(message: "添加成功!")
                self.navigationController?.popViewController(animated: true)
            }, fail: { (code, msg) in
                print("sbbbd失败")
            }) { }
        }
        
    }
 
}
